<?php

class Admin_UserController extends Ext_Controller_Action_Admin
{
    public function init()
    {
        parent::init();
    }
    
    public function indexAction()
    {
         $this->_forward('search');
    }
    
    public function searchAction()
    {
        $username = $this->filter->filter($this->_request->get('username'));
        $domain   = $this->filter->filter($this->_request->get('domain'));
        
        $users = array();
        if($username)
        {
            $userAccountBaseUsername = App_Model_DbTable_User_Account_Base_Username::getInstance()->getRowByUsername($username);
            if(isset($userAccountBaseUsername['userId']))
            {
                $userId = $userAccountBaseUsername['userId'];
                $users[] = Ext_User::getInstance()->show($userId)->buildAccount()->buildUrl();
            }
            
            $this->view->username = $username;
        }
        elseif($domain)
        {
            $domainOne = App_Model_DbTable_Domain::getInstance()->getRowByDomain($domain);
            if(isset($domainOne['userId']))
            {
                $userId = $domainOne['userId'];
                $users[] = Ext_User::getInstance()->show($userId)->buildAccount()->buildUrl();
            }
            
            $this->view->domain = $domain;
        }
        else
        {
            $count = App_Model_User::getInstance()->getUserCount();
            $list = App_Model_User::getInstance()->getUserList();
            
            $adapter = new Zend_Paginator_Adapter_DbSelect($list);
            $adapter->setRowCount($count);
            $paginator = new Zend_Paginator($adapter);
              $paginator
                  //->setCurrentPageNumber(1)
                  ->setItemCountPerPage(2)
                  ->setCurrentPageNumber($this->_getParam('page'))
                //->setDefaultPageRange(20)
                ;
    
            $users = array();
            foreach ($paginator as $key=>$item)
            {
                $userId = $item['id'];

                $users[] = Ext_User::getInstance()->show($userId)->buildAccount()->buildUrl();
            }
              
            $this->view->paginator = $paginator;
            $this->view->requestParams  = $this->_request->getParams();
        }
        
        $this->view->users = $users;
        
    }
    
    public function showUserPasswordFormAction()
    {
        $userId = $this->filter->filter($this->_request->get('user-id'));
        
        $user = Ext_User::getInstance()->show($userId)->buildAccount();
        
        $viewForm = new Ext_View_Form_Admin_User_Password();
        if(isset($user->userId))
        {
            $viewForm->getElement('userId')->setValue($user->userId);
        }
        
        $this->view->user = $user;
        $this->view->form = $viewForm;
    }
    
    public function saveUserPasswordPostAction()
    {
        $viewResult = new Ext_View_Result();

        $userId   = $this->filter->filter($this->_request->getPost('userId'));
        $password = $this->filter->filter($this->_request->getPost('password'));
        
        $editPassword = new Ext_User_Edit_Password($userId);
        $editResult = $editPassword->edit($password);
        if(!$editResult->isValid()){
            $viewResult->addMessages($editResult->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $viewResult->addMessage($this->translater->translate("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function showUserEmailFormAction()
    {
        $userId = $this->filter->filter($this->_request->get('user-id'));
        
        $user = Ext_User::getInstance()->show($userId)->buildAccount();
        
        $viewForm = new Ext_View_Form_Admin_User_Email();
        if(isset($user->userId))
        {
            $viewForm->getElement('userId')->setValue($user->userId);
        }
        if(isset($user->account->email))
        {
            $viewForm->getElement('email')->setValue($user->account->email);
        }
        $this->view->user = $user;
        $this->view->form = $viewForm;

    }
    
    public function saveUserEmailPostAction()
    {
        $viewResult = new Ext_View_Result();

        $userId = $this->filter->filter($this->_request->getPost('userId'));
        $email  = $this->filter->filter($this->_request->getPost('email'));
        
        $editEmail = new Ext_User_Edit_Email($userId);
        $editResult = $editEmail->edit($email);
        if(!$editResult->isValid()){
            $viewResult->addMessages($editResult->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $viewResult->addMessage($this->translater->translate("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }

    public function showUserFormAction()
    {

        $userId = $this->filter->filter($this->_request->get('user-id'));
        
        $user = Ext_User::getInstance()->show($userId)->buildRole();
        
        $viewForm = new Ext_View_Form_Admin_User();
        if(isset($user->userId))
        {
            $viewForm->getElement('userId')->setValue($user->userId);
        }
        if(isset($user->nickname))
        {
            $viewForm->getElement('nickname')->setValue($user->nickname);
        }
        $roleAllArray = App_Model_DbTable_Acl_Role::getInstance()->getAll();
        $_options = array();
        for($i=0;$i<count($roleAllArray);$i++)
        {
            $__roleId = $roleAllArray[$i]['id'];
            $_options[$__roleId] = $roleAllArray[$i]['roleName'];
        }
        $viewForm->getElement('roleId')->addMultiOption(null, null);
        $viewForm->getElement('roleId')->addMultiOptions($_options);
        if(isset($user->role->roleId))
        {
            $viewForm->getElement('roleId')->setValue($user->role->roleId);
        }
        
        $this->view->user = $user;
        $this->view->form = $viewForm;

    }
    
    public function saveUserPostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $userId   = $this->filter->filter($this->_request->getPost('userId'));
        $roleId   = $this->filter->filter($this->_request->getPost('roleId'));
        $nickname = $this->filter->filter($this->_request->getPost('nickname'));

        $row = array(
            'roleId'   => $roleId,
            'nickname' => $nickname,
        );
        App_Model_DbTable_User::getInstance()->addOld($row, $userId);

        $viewResult->addMessage($this->translater->translate("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
}

